#include "engineSettings.h"

namespace Engine
{

// screen settings
ScreenSettings screenSettings = ScreenSettings();

// file structure
FileStructure fileStructure = FileStructure();

// font settings
FontSettings fontSettings = FontSettings();

// gui settings
GuiSettings guiSettings = GuiSettings();

// map file structure
MapFileStruct mapFileStruct = MapFileStruct();

// console settings
ConsoleSettings consoleSettings = ConsoleSettings();

// camera settings
CameraSettings cameraSettings = CameraSettings();

// loading settings from xml file
// using pugixml parser
void loadSettings(std::string file, unsigned int flags)
{
	pugi::xml_node settings;

	// open file
#ifdef _DEBUG
	std::cout << "SETTINGS: Loading " << file << " settings file." << std::endl;
#endif
	pugi::xml_document doc;

	pugi::xml_parse_result result = doc.load_file(file.c_str());

	if (result)
		std::cout << "SETTINGS: XML " << file << " parsed without errors." << std::endl;
	else
	{
		std::cerr << "SETTINGS: XML" << file << "parsed with errors" << std::endl;
		std::cerr << "SETTINGS: Error description: " << result.description() << "\n";
		std::cerr << "SETTINGS: Error offset: " << result.offset << " error at ..."
				<< (file.c_str() + result.offset) << std::endl;
		return;
	}

	// load screen settings
	if ((flags & RTS_LOAD_ALL) || (flags & RTS_LOAD_SCREEN))
	{
		// load settings
		settings = doc.child("settings").child("screen");

		// load screen settings
		screenSettings.screenWidth =
				settings.child("size").attribute("width").as_int();
		screenSettings.screenHeight =
				settings.child("size").attribute("height").as_int();
		screenSettings.screenBpp =
				settings.child("bpp").attribute("value").as_int();
		screenSettings.fullscreen =
				settings.child("fullscreen").attribute("value").as_bool();
	}

	// load console
	if ((flags & RTS_LOAD_ALL) || (flags & RTS_LOAD_CONSOLE))
	{
		// load settings
		settings = doc.child("settings").child("console");

		// load console
		consoleSettings.logFile =
				settings.child("logFile").attribute("value").as_string();
		consoleSettings.commandFile = settings.child("commandFile").attribute(
				"value").as_string();
		consoleSettings.consolePath = settings.child("consolePath").attribute(
				"value").as_string();

		consoleSettings.consoleFont =
				settings.child("font").attribute("value").as_string();
		consoleSettings.consoleBackground = settings.child("background").attribute(
				"value").as_string();
	}

	settings = doc.child("settings").child("filestructure");

	// load shader
	if ((flags & RTS_LOAD_ALL) || (flags & RTS_LOAD_SHADER))
		fileStructure.shaderPath =
				settings.child("shaderPath").attribute("value").as_string();

	// load script
	if ((flags & RTS_LOAD_ALL) || (flags & RTS_LOAD_SCRIPT))
		fileStructure.scriptPath =
				settings.child("scriptPath").attribute("value").as_string();

	// load model
	if ((flags & RTS_LOAD_ALL) || (flags & RTS_LOAD_MODEL))
		fileStructure.modelsPath =
				settings.child("modelsPath").attribute("value").as_string();

	// load texture
	if ((flags & RTS_LOAD_ALL) || (flags & RTS_LOAD_TEXTURE)
			|| (flags & RTS_LOAD_CONSOLE) || (flags & RTS_LOAD_GADGETS)
			|| (flags & RTS_LOAD_MAP) || (flags & RTS_LOAD_FONTS))
		fileStructure.texturePath =
				settings.child("texturePath").attribute("value").as_string();

	// load gui
	if ((flags & RTS_LOAD_ALL) || (flags & RTS_LOAD_GADGETS))
	{
		settings = doc.child("settings").child("GUI");
		guiSettings.gadgetsPath =
				settings.child("gadgetsPath").attribute("value").as_string();
		guiSettings.guiScript =
				settings.child("guiScript").attribute("value").as_string();
	}

	// load fonts
	if ((flags & RTS_LOAD_ALL) || (flags & RTS_LOAD_FONTS))
	{
		settings = doc.child("settings").child("font");
		fontSettings.fontPath =
				settings.child("fontPath").attribute("value").as_string();
		fontSettings.fontFile =
				settings.child("fontFile").attribute("value").as_string();
	}

	// load map file structure
	if ((flags & RTS_LOAD_ALL) || (flags & RTS_LOAD_MAP))
	{
		settings = doc.child("settings").child("mapFileStructure");
		mapFileStruct.terrainPath =
				settings.child("terrainPath").attribute("value").as_string();
		mapFileStruct.waterPath =
				settings.child("waterPath").attribute("value").as_string();
		mapFileStruct.skyPath =
				settings.child("skyPath").attribute("value").as_string();
	}

	// load camera settings
	if ((flags & RTS_LOAD_ALL) || (flags & RTS_LOAD_CAMERA))
	{
		settings = doc.child("settings").child("camera");
		cameraSettings.MaxZoom =
				settings.child("MaxZoom").attribute("value").as_float();
		cameraSettings.MinZoom =
				settings.child("MinZoom").attribute("value").as_float();
		cameraSettings.MaxStep =
				settings.child("MaxStep").attribute("value").as_int();
		cameraSettings.Deceleration = settings.child("Deceleration").attribute(
				"value").as_int();
		cameraSettings.EdgeSens =
				settings.child("EdgeSens").attribute("value").as_int();
	}

}
}
